//创建路由的对象，声明路由的规则
import {createRouter, createWebHistory} from "vue-router";
import Login from "../components/Login.vue";
import Register from "../components/Register.vue";
import ShowSchedule from "../components/ShowSchedule.vue";
import pinia from "../pinia.js";
import {defineUser} from "../store/userStore.js";
let sysUser = defineUser(pinia)

const router = createRouter({
    //createWebHistory 记录历史的页面，能够在页面中实现页面的前进和后退
    history:createWebHistory(),
    //声明路由的地址，实现地址的跳转
    routes:[

        {
            path:'/',
            component:Login
        },
        {
            path:'/login',
            component:Login
        },
        {
            path:'/regist',
            component:Register
        },
        {
            path:'/showSchedule',
            component:ShowSchedule
        },
    ]
})
//定义一个路由守卫，当检测到登录状态下才可以访问到主页面
router.beforeEach((to, from, next) => {
    if (to.path=='/showSchedule'){
        //检测有咩有用户登录
        if (sysUser.username==''){
            alert('您尚未登录,请登录后再查看日程')
            next('/login')
        }else {
            next()
        }
    }else {
        next()
    }
})
//暴露对象
export default router;